package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.SkillGroupAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.SkillBean;
import com.jucai.pppeiyouclient.bean.SkillGroupBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.taglayout.TagLayout;

import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 技能认证
 * 2017-5-14 12:08:09 修改
 */
public class SkillAuthenticateActivity extends BaseActivity {
    private static final String TAG = "SkillAuthenticateActivity :";
    private RecyclerView skillGroupRv;
    private List<SkillGroupBean> skillGroups = new ArrayList<>();
    private SkillGroupAdapter groupAdapter;
    private View headerView, footerView;
    private TagLayout moreSkill;
    private EditText moreInput;
    private TextView moreSkillAdd, skillSubmit;
    private int skillChoosePosition = 0;

    private boolean getBoolean;
    private int type;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_rv);
        ((TextView) findViewById(R.id.common_title_text)).setText("添加技能认证");
        groupAdapter = new SkillGroupAdapter(SkillAuthenticateActivity.this, skillGroups);
        skillGroupRv = (RecyclerView) findViewById(R.id.common_view);
        skillGroupRv.setLayoutManager(new LinearLayoutManager(SkillAuthenticateActivity.this, LinearLayoutManager.VERTICAL, false));
        ((LinearLayoutManager) skillGroupRv.getLayoutManager()).setReverseLayout(false);
        skillGroupRv.setItemAnimator(new DefaultItemAnimator());
        skillGroupRv.setAdapter(groupAdapter);

        headerView = LayoutInflater.from(SkillAuthenticateActivity.this).inflate(R.layout.head_skill_group, skillGroupRv, false);
        footerView = LayoutInflater.from(SkillAuthenticateActivity.this).inflate(R.layout.footer_skill_group, skillGroupRv, false);
        groupAdapter.setHeader(headerView);
        groupAdapter.setBottom(footerView);

        moreSkill = (TagLayout) footerView.findViewById(R.id.more_skill);
        moreInput = (EditText) footerView.findViewById(R.id.more_input);
        moreSkillAdd = (TextView) footerView.findViewById(R.id.more_skill_add);
        skillSubmit = (TextView) footerView.findViewById(R.id.skills_submit);
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {//只选一个技能
            ((TextView) findViewById(R.id.common_title_text)).setText("选择技能");
            headerView.setVisibility(View.INVISIBLE);
            footerView.setVisibility(View.INVISIBLE);
            groupAdapter.setOnSelectedSkillListener(new SkillGroupAdapter.OnSelectedSkillListener() {
                @Override
                public void onClick(SkillBean skillBean) {
                    Intent intent = new Intent();
                    intent.putExtra("skill_id", skillBean.getSkill_id());
                    intent.putExtra("skill_name", skillBean.getSkill_name());
                    SkillAuthenticateActivity.this.setResult(RESULT_OK, intent);
                    SkillAuthenticateActivity.this.finish();
                }
            });
        }
        getBoolean = true;

    }

    @Override
    protected void loadNetData() {

        if (!getBoolean) {
            return;
        }

        getBoolean = false;

        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Skill.getSkillList");

        CommonRequestNetData.getJsonArrayData(SkillAuthenticateActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                List<SkillGroupBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SkillGroupBean>>() {
                }.getType());
                if (list != null && list.size() > 0){
                    skillGroups.clear();
                    skillGroups.addAll(list);
                    groupAdapter.setData(skillGroups);
                }
            }
        });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        moreSkillAdd.setOnClickListener(this);
        skillSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(SkillAuthenticateActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.more_skill_add:
                if (!TextUtils.isEmpty(moreInput.getText().toString())) {
                    LogUtils.e("do add skill");
                    for (int i = 0; i < 5; i++) {

                        final TextView newSkill = new TextView(SkillAuthenticateActivity.this);
                        newSkill.setText("技能技能");
                        newSkill.setBackgroundResource(R.drawable.shape_round_corner_orange_4dp);
                        newSkill.setPadding(10, 5, 10, 5);
                        newSkill.setTextSize(14);
                        newSkill.setTextColor(Color.WHITE);
                        moreSkill.addView(newSkill);
                        LogUtils.e("newSkill == null ---> " + (newSkill == null) +
                                ",moreSkill == null ---> " + (moreSkill == null));

                        newSkill.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                newSkill.setVisibility(View.GONE);
                            }
                        });
                    }

                    moreInput.setText("");

                }
                break;

            case R.id.skills_submit:
                if (getIntent().getIntExtra("getOrderListToSkill", 0) == 1) {
                    finish();
                } else {
                    Gson gson = new Gson();
                    List<Map<String, String>> list = new ArrayList<>();
                    for (int i = 0; i < groupAdapter.getSelectList().size(); i++) {
                        Map<String, String> map = new HashMap<>();
                        map.put("skill_id", groupAdapter.getSelectList().get(i).getSkill_id());
                        map.put("skill_sn", groupAdapter.getSelectList().get(i).getSkill_sn());
                        map.put("skill_name", groupAdapter.getSelectList().get(i).getSkill_name());
                        map.put("istag", "false");
                        list.add(map);
                    }
                    String json = gson.toJson(list);

                    HashMap<String, String> param = new HashMap<>();
                    param.put("service", "Accompany_Skill.certSkills");
                    param.put("skill_cert_list", StringEscapeUtils.escapeJava(json));
                    param.put("accompany_id", SharedPreferencesUtil.getStringPreference(this, "PREFERENCE", "accompany_id", ""));
                    param.put("member_id", LoginUtils.getMember_id(this));
                    param.put("token", LoginUtils.getToken(this));
                    LogUtils.e(TAG + "param = " + param.toString());

                    CommonRequestNetData.getJsonStringData(SkillAuthenticateActivity.this, param, false, new CommonRequestNetData.JsonStringCallBack() {

                        @Override
                        public void parseJsonString(String str) {
                            LogUtils.e(str);
                            ToastUtils.showToast(str);
                            ActivityCollector.removeActivity(SkillAuthenticateActivity.this);
                        }
                    });
                }
                break;
        }
    }
}
